package a2022.a20221019;

import java.util.Arrays;

/**
 * @author lenovo
 * @date 2022/10/19
 */
public class a {
    public static void main(String[] args) {
        System.out.println(countStudents(
                new int[]{1, 1, 0, 0},
                new int[]{0, 1, 0, 1}));
        //new int[]{-1, 1, 1, -1, -1, 1}
    }

    public static int countStudents2(int[] students, int[] sandwiches) {
        int len = students.length;
        int num = 0;
        int x = -1;
        for (int i = 0; i < len; i++) {
            if (sandwiches[i] == students[i]) {
                students[i] = -1;
            } else {
                num = 0;
                while (num < len) {
                    if (students[num] == sandwiches[i]) {
                        students[num] = -1;
                        x = 0;
                        break;
                    }
                    num++;
                }
                if (x != 0) {
                    num = i;
                    break;
                }
                x = -1;
            }
        }
        Arrays.sort(students);
        return students[len - 1] == -1 ? 0 : len - num;
    }

    public static int countStudents(int[] a, int[] b) {
        int[] cnts = new int[2];
        for (int x : a) cnts[x]++;
        for (int i = 0; i < b.length; i++) {
            if (--cnts[b[i]] == -1) return b.length - i;
        }
        return 0;
    }
}
